package org.example.im.common.idle;

import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.timeout.IdleStateEvent;
import io.netty.handler.timeout.IdleStateHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @author dingrui
 * @since 2022/2/12
 * @description 空闲检测
 */
@Slf4j
@Component
public class IdleHandler extends IdleStateHandler {

    private static final int READER_IDLE_TIME = 300_000;

    public IdleHandler() {
        super(READER_IDLE_TIME, 0, 0, TimeUnit.MILLISECONDS);
    }

    @Override
    protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception {
        log.info("[Idle] 即将关闭连接 超过[{}s]的空闲时间没有读到数据", READER_IDLE_TIME);
        ctx.channel().close();
    }
}
